package cn.tedu.egbank.account.controller;

import cn.tedu.egbank.account.mapper.AccountMapper;
import cn.tedu.egbank.account.pojo.dto.AddAccountParam;
import cn.tedu.egbank.account.pojo.entity.Account;
import cn.tedu.egbank.account.pojo.vo.AccountVO;
import cn.tedu.egbank.account.pojo.vo.AllOfAccountVO;
import cn.tedu.egbank.base.response.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

@Slf4j
@Tag(name = "02-账户模块" , description = "20")
@RestController
@RequestMapping("/api/accounts/")
public class AccountController {
    @Autowired
    private AccountMapper accountMapper;

    @PostMapping("add")
    @Operation(summary = "添加账户")
    @ApiOperationSupport(order = 10)
    public JsonResult addAccount(@RequestBody AddAccountParam addAccountParam){
        log.debug("addAccountParam=" + addAccountParam);
        Account account = new Account();
        account.setCustomerId( addAccountParam.getCustomerId());
        account.setType(Account.AccountType.valueOf(addAccountParam.getType().name()));

        log.debug("account=" + account);

        // 1. 利用时间戳生成账户编号前n位
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        long timestamp = System.currentTimeMillis();
        sb.append(timestamp);

        // 2. 补充随机数字到20位
        int remaining = 20 - sb.length();
        for (int i = 0; i < remaining; i++) {
            // 生成0-9的随机数字
            sb.append(random.nextInt(10));
        }
        account.setAccountNumber(sb.toString());
        account.setCreatedAt(new Date());

        int rows = accountMapper.insertAccount(account);
        AccountVO accountVO = accountMapper.selectNewAccount(account);
        return JsonResult.ok(accountVO);
    }

    @GetMapping("list")
    @Operation(summary = "获取所有账户信息")
    @ApiOperationSupport(order = 20)
    public JsonResult listAccounts() {
        log.debug("开始处理【获取所有账户信息】的请求，无参数");
        ArrayList<AccountVO> list = accountMapper.selectAll();
        if(list == null){
            return JsonResult.fail("暂无账户信息");
        }
        return JsonResult.ok(list);
    }

    @PostMapping("{accountId}/del")
    @Operation(summary = "删除指定账户")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteAccount(@PathVariable("accountId") Integer accountId){
        log.debug("开始处理【删除指定账户】的请求，参数：{}", accountId);
        int rows = accountMapper.deleteAccount(accountId);
        if (rows > 0){
            return JsonResult.ok();
        }else {
            return JsonResult.fail("用户不存在");
        }
    }

    @GetMapping("{accountId}/transactions")
    @Operation(summary = "获取指定账户的所有交易记录")
    @ApiOperationSupport(order = 40)
    public JsonResult listTransactions(@PathVariable("accountId") Integer accountId){
        log.debug("开始处理【获取指定账户的所有交易记录】的请求，参数：{}", accountId);
        AllOfAccountVO allOfAccountVO = accountMapper.selectAllById(accountId);

        if (allOfAccountVO == null){
            return JsonResult.fail("暂无该账户或者该账户未曾交易");
        }

        return JsonResult.ok(allOfAccountVO);
    }



}














































